package com.xuexiang.xuidemo.view;

import ohos.agp.components.Component;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.xuexiang.xui_lib.component.banner.transform.PageTransformer;

public abstract class ABaseTransformer implements PageTransformer {

    protected final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP,0x999999,"ABaseTransformer");

    static float min(float value,float m){
        return Math.max(value, m);
    }

    @Override
    public void transformPage(Component component, float position) {
        float clampedPosition = clampPosition(position);
        onPreTransform(component,clampedPosition);
        onTransform(component, clampedPosition);
        onPostTransform(component, clampedPosition);
    }

    protected void onPreTransform(Component component , float position){
        float width = (float)component.getWidth();
        component.setRotation(0.0F);
        component.setScaleX(1.0F);
        component.setScaleY(1.0F);
        component.setPivotX(0.0F);
        component.setPivotY(0.0F);
        component.setTranslationY(0.0F);
        component.setTranslationX(isPagingEnabled() ? 0.0F : -width * position);
        if (this.hideOffscreenPages()) {
            component.setAlpha(position > -1.0F && position < 1.0F ? 1.0F : 0.0F);
            component.setEnabled(false);
        } else {
            component.setEnabled(true);
            component.setAlpha(1.0F);
        }
    }

    private float clampPosition(float position){
        float clampPosition;
        if (position < -1.0F) {
            clampPosition = -1.0F;
        } else if (position > 1.0F) {
            clampPosition = 1.0F;
        } else {
            clampPosition = Float.isNaN(position) ? 0.0F : position;
        }

        return clampPosition;
    }

    public boolean isPagingEnabled() {
        return false;
    }

    protected boolean hideOffscreenPages(){
        return true;
    }

    protected void onPostTransform(Component component,float position){}
    protected abstract void onTransform(Component component,float position);
}

